﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Collections.Generic;
using System.IO;
using System.Threading;

namespace AudioEffectBox
{
    public class AudioGeneratorStreamSource : MediaStreamSource
    {
        private MediaStreamDescription _mediaStreamDescription;
        private long _currentTimeStamp = 0L;
        private Audio16BufferStream _bufferStream;
        
        public Audio16BufferStream Buffer { get; set; }

        public AudioGeneratorStreamSource(Audio16BufferStream _buffer)
        {
            _bufferStream = _buffer;
        }
        protected override void CloseMedia()
        {
            //ok
        }

        protected override void GetDiagnosticAsync(MediaStreamSourceDiagnosticKind diagnosticKind)
        {
            throw new NotImplementedException();
        }

        protected override void GetSampleAsync(MediaStreamType mediaStreamType)
        {                                   
            Stream _stmSamples;
            while ((_stmSamples = _bufferStream.GetSamples()) == null)
            {
                Thread.Sleep(5);
            }
            Dictionary<MediaSampleAttributeKeys, string> _sampleAttributes = new Dictionary<MediaSampleAttributeKeys, string>();
            MediaStreamSample _sample = new MediaStreamSample(_mediaStreamDescription,_stmSamples,0,_stmSamples.Length,_currentTimeStamp,
                _sampleAttributes);
            _currentTimeStamp += _stmSamples.Length * 2500000L / 22050;
            this.ReportGetSampleCompleted(_sample);
        }

        protected override void OpenMediaAsync()
        {
            string _hexWaveFormatExString =
               "0100" + //PCM
               "0200" + // 2 Channels
              "22560000" + // 22050 Samples / sec
              "88580100" + //22050*4 byte rate
               "0200" + //Block align
               "1000" + // 16 Bits per samples
               "0000";
           List<MediaStreamDescription> _streamDescriptions = new List<MediaStreamDescription>();
           Dictionary<MediaStreamAttributeKeys,string> _streamAttributes = new Dictionary<MediaStreamAttributeKeys,string>();
           _streamAttributes.Add(MediaStreamAttributeKeys.CodecPrivateData, _hexWaveFormatExString);
           _mediaStreamDescription = new MediaStreamDescription(MediaStreamType.Audio, _streamAttributes);
           _streamDescriptions.Add(_mediaStreamDescription);  

           Dictionary<MediaSourceAttributesKeys,string> _sourceAttributes = new Dictionary<MediaSourceAttributesKeys,string>();
           _sourceAttributes.Add(MediaSourceAttributesKeys.Duration, "0");
           _sourceAttributes.Add(MediaSourceAttributesKeys.CanSeek, "false");
           
                     
            ReportOpenMediaCompleted(_sourceAttributes, _streamDescriptions);
        }

        protected override void SeekAsync(long seekToTime)
        {
            this.ReportSeekCompleted(seekToTime);
        }

        protected override void SwitchMediaStreamAsync(MediaStreamDescription mediaStreamDescription)
        {
            throw new NotImplementedException();
        }
    }
}
